<template>
  <el-card class="hot-search">
    <!-- 搜索框部分 -->
    <div class="search-box">
      <div class="search-engines">
        <button 
          v-for="engine in searchEngines" 
          :key="engine.name"
          :class="['engine-btn', { active: currentEngine === engine.name }]"
          @click="currentEngine = engine.name"
        >
          <i :class="engine.icon"></i>
          {{ engine.label }}
        </button>
      </div>
      <div class="search-input">
        <input 
          v-model="searchText" 
          :placeholder="`在 ${getCurrentEngine.label} 中搜索...`"
          @keyup.enter="handleSearch"
        >
        <button class="search-btn" @click="handleSearch">
          <i class="fas fa-search"></i>
        </button>
      </div>
    </div>

    <!-- 热搜列表部分 -->
    <div class="hot-lists">
      <div class="hot-tabs">
        <button 
          v-for="tab in hotTabs" 
          :key="tab.type"
          :class="['tab-btn', { active: currentTab === tab.type }]"
          @click="currentTab = tab.type"
        >
          <i :class="tab.icon"></i>
          {{ tab.label }}
        </button>
      </div>
      
      <div class="hot-content">
        <div class="hot-grid" v-loading="loading">
          <div 
            v-for="(item, index) in currentHotList" 
            :key="index" 
            class="hot-card"
            @click="handleHotItemClick(item)"
          >
            <span class="rank" :class="getRankClass(index)">{{ index + 1 }}</span>
            <div class="card-content">
              <a :href="item.url" target="_blank" class="title">{{ item.keyword }}</a>
              <div class="card-footer">
                <span class="hot-value" v-if="item.hotValue">
                  <i class="fas fa-fire"></i>
                  {{ formatHotValue(item.hotValue) }}
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import { getHotListApi } from '@/api/hot-search'
export default {
  name: 'HotSearch',
  data() {
    return {
      searchText: '',
      currentEngine: 'baidu',
      currentTab: 'weibo',
      loading: false,
      searchEngines: [
        { name: 'baidu', label: '百度', icon: 'fab fa-baidu', url: 'https://www.baidu.com/s?wd=' },
        { name: 'google', label: '谷歌', icon: 'fab fa-google', url: 'https://www.google.com/search?q=' },
        { name: 'gitee', label: '码云', icon: 'fab fa-git-alt', url: 'https://search.gitee.com/?q=' },
        { name: 'github', label: 'GitHub', icon: 'fab fa-github', url: 'https://github.com/search?q=' }
      ],
      hotTabs: [
        { type: 'weibo', label: '微博热搜', icon: 'fab fa-weibo' },
        { type: 'zhihu', label: '知乎热榜', icon: 'fab fa-zhihu' },
        { type: 'toutiao', label: '头条热榜', icon: 'fas fa-fire' },
        { type: 'baidu', label: '百度热搜', icon: 'fas fa-chart-line' },
        { type: 'csdn', label: 'CSDN热榜', icon: 'fas fa-code' }
      ],
      hotLists: {
        weibo: [],
        zhihu: [],
        toutiao: [],
        baidu: [],
        csdn: []
      }
    }
  },
  computed: {
    getCurrentEngine() {
      return this.searchEngines.find(engine => engine.name === this.currentEngine)
    },
    currentHotList() {
      return this.hotLists[this.currentTab] || []
    }
  },
  methods: {
    handleSearch() {
      if (!this.searchText.trim()) return
      const engine = this.getCurrentEngine
      window.open(engine.url + encodeURIComponent(this.searchText), '_blank')
    },
    getRankClass(index) {
      if (index < 3) return `rank-${index + 1}`
      return ''
    },
    formatHotValue(value) {
      if (value >= 10000) {
        return (value / 10000).toFixed(1) + 'w'
      }
      return value
    },
    async fetchHotList(type) {
      this.loading = true
      try {
        // 这里替换为实际的API调用
        const res = await getHotListApi(type)
        this.hotLists[type] = res.data.data
      } catch (error) {
        this.$message.error('获取热搜数据失败')
      } finally {
        this.loading = false
      }
    },
    handleHotItemClick(item) {
      if (item.url) {
        window.open(item.url, '_blank')
      }
    }
  },
  watch: {
    currentTab: {
      immediate: true,
      handler(newTab) {
        this.fetchHotList(newTab)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.hot-search {
  max-width: 1200px;
  margin: $spacing-xl auto;
  .search-box {
    margin-bottom: $spacing-xl;

    .search-engines {
      display: flex;
      gap: $spacing-sm;
      margin-bottom: $spacing-md;

      .engine-btn {
        padding: $spacing-sm $spacing-lg;
        border: none;
        background: var(--hover-bg);
        color: var(--text-secondary);
        border-radius: $border-radius-md;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        gap: $spacing-xs;

        &.active {
          background: $primary;
          color: white;
        }

        &:hover:not(.active) {
          color: $primary;
        }
      }
    }

    .search-input {
      position: relative;
      
      input {
        width: 100%;
        padding: $spacing-md $spacing-xl;
        padding-right: 50px;
        border: 2px solid var(--border-color);
        border-radius: $border-radius-md;
        background: var(--card-bg);
        color: var(--text-primary);
        font-size: 1em;
        transition: all 0.3s ease;

        &:focus {
          outline: none;
          border-color: $primary;
        }
      }

      .search-btn {
        position: absolute;
        right: $spacing-sm;
        top: 50%;
        transform: translateY(-50%);
        background: none;
        border: none;
        color: var(--text-secondary);
        cursor: pointer;
        padding: $spacing-sm;
        transition: all 0.3s ease;

        &:hover {
          color: $primary;
        }
      }
    }
  }

  .hot-lists {
    .hot-tabs {
      display: flex;
      gap: $spacing-sm;
      margin-bottom: $spacing-lg;
      border-bottom: 1px solid var(--border-color);
      padding-bottom: $spacing-sm;
      overflow-x: auto;
      scrollbar-width: none;
      -ms-overflow-style: none;
      
      &::-webkit-scrollbar {
        display: none;
      }

      .tab-btn {
        padding: $spacing-sm $spacing-lg;
        border: none;
        background: none;
        color: var(--text-secondary);
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        gap: $spacing-xs;
        white-space: nowrap;
        flex-shrink: 0;

        i {
          font-size: 1.1em;
        }

        &.active {
          color: $primary;
          position: relative;

          &::after {
            content: '';
            position: absolute;
            bottom: -$spacing-sm - 1px;
            left: 0;
            width: 100%;
            height: 2px;
            background: $primary;
          }
        }

        &:hover:not(.active) {
          color: $primary;
        }
      }
    }

    .hot-content {
      padding: $spacing-md 0;

      .hot-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
        gap: $spacing-md;
        padding: $spacing-sm;

        .hot-card {
          position: relative;
          background: var(--card-bg);
          border: 1px solid var(--border-color);
          border-radius: $border-radius-md;
          padding: $spacing-md $spacing-lg;
          transition: all 0.3s ease;
          display: flex;
          gap: $spacing-md;
          cursor: pointer;
          
          &:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            border-color: var(--hover-color);

            .title {
              color: $primary;
            }
          }

          .rank {
            width: 24px;
            height: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 4px;
            font-size: 0.9em;
            color: var(--text-secondary);
            flex-shrink: 0;

            &.rank-1 { 
              background: #ff4757; 
            }
            &.rank-2 { 
              background: #ff6b81; 
            }
            &.rank-3 { 
              background: #ff7f50; 
            }
            
            .zhihu & {
              &.rank-1, &.rank-2, &.rank-3 { 
                background: #0084ff; 
                box-shadow: 0 2px 8px rgba(0, 132, 255, 0.3);
              }
            }
            
            .toutiao & {
              &.rank-1, &.rank-2, &.rank-3 { 
                background: #ff2442; 
                box-shadow: 0 2px 8px rgba(255, 36, 66, 0.3);
              }
            }
          }

          .card-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: $spacing-sm;

            .title {
              color: var(--text-primary);
              text-decoration: none;
              font-size: 0.95em;
              line-height: 1.4;
              display: -webkit-box;
              -webkit-line-clamp: 2;
              -webkit-box-orient: vertical;
              overflow: hidden;
              transition: color 0.3s ease;

              &:hover {
                text-decoration: none;
              }
            }

            .card-footer {
              display: flex;
              align-items: center;
              justify-content: space-between;
              margin-top: auto;
              font-size: 0.85em;
              color: var(--text-secondary);

              .hot-value {
                display: flex;
                align-items: center;
                gap: 4px;
                
                i {
                  color: #ff6b6b;
                }
              }
            }
          }
        }
      }
    }
  }
}

@include responsive(sm) {
  .hot-search {
    margin: $spacing-md;
    padding: $spacing-lg;

    .hot-lists {
      .hot-content {
        .hot-grid {
          grid-template-columns: 1fr;
          gap: $spacing-sm;
        }
      }
    }
  }
}
</style> 